#pragma once
#include<iostream>
#include<assert.h>
using namespace std;

namespace baka
{
	template<class T>
	class my_vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;

		iterator begin()
		{
			return _start;
		}
		iterator end()
		{
			return _finish;
		}
		const_iterator begin() const
		{
			return _start;
		}
		const_iterator end() const
		{
			return _finish;
		}

		my_vector()
		{

		}
		/*my_vector(int n, const T& value = T())
		{
			_start = new T[n];
			_finish = _start;
			_end_of_storage = _start + n;
			while (_finish != _end_of_storage)
			{
				*_finish = value;
				_finish++;
			}
		}*/
		my_vector(int n, const T& value = T())
		{
			reserve(n);
			for (int i = 0; i < n; i++)
			{
				push_back(value);
			}
		}

		my_vector(size_t n, const T& value = T())
		{
			reserve(n);
			for (int i = 0; i < n; i++)
			{
				push_back(value);
			}
		}
		/*template<class InputIterator>
		my_vector(InputIterator first, InputIterator last)
		{
			size_t size = last - first;
			_start = new T[size];
			_finish = _start;
			_end_of_storage = _start + size;
			while (_finish != _end_of_storage)
			{
				*_finish = *first;
				_finish++;
				first++;
			}
		}*/
		template<class InputIterator>
		my_vector(InputIterator first, InputIterator last)
		{
			while (first != last)
			{
				push_back(*first);
				first++;
			}
		}
		/*my_vector(const my_vector<T>& v)
		{
			size_t size = v.cend() - v.cbegin();
			_start = new T[size];
			_finish = _start;
			_end_of_storage = _start + size;
			const_iterator it = v.cbegin();
			while (_finish != _end_of_storage)
			{
				*_finish = *it;
				_finish++;
				it++;
			}
		}*/
		my_vector(const my_vector<T>& v)
		{
			reserve(v.size());
			for (auto& e : v)
			{
				push_back(e);
			}
		}

		/*my_vector<T>& operator= (my_vector<T> v)
		{
			delete[]_start;
			size_t size = v.end() - v.begin();
			_start = new T[size];
			_finish = _start;
			_end_of_storage = _start + size;
			const_iterator it = v.begin();
			while (_finish != _end_of_storage)
			{
				*_finish = *it;
				_finish++;
				it++;
			}
			return *this;
		}*/
		my_vector<T>& operator= (my_vector<T> v)
		{
			swap(v);
			return *this;
		}
		~my_vector()
		{
			delete[] _start;
			_start = _finish = _end_of_storage = nullptr;
		}

		size_t size() const
		{
			return _finish - _start;
		}

		size_t capacity() const
		{
			return _end_of_storage - _start;
		}

		void swap(my_vector<T>& v)
		{
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_end_of_storage, v._end_of_storage);
		}

		void reserve(size_t n)
		{
			if (n > capacity())
			{
				my_vector<T> tmp;
				tmp._start = new T[n];
				tmp._finish = tmp._start + size();
				tmp._end_of_storage = tmp._start + n;
				memcpy(tmp._start, _start, size()*sizeof(T));
				swap(tmp);
			}
		}

		void resize(size_t n, const T& value = T())
		{
			if (n <= size())
			{
				_finish = _start + n;
				_end_of_storage = _finish;
			}
			else
			{
				reserve(n);
				while (_finish!= _start + n)
				{
					*_finish = value;
					_finish++;
				}
			}
		}

		T& operator[](size_t pos)
		{
			return *(_start + pos);
		}
		const T& operator[](size_t pos)const
		{
			return *(_start + pos);
		}

		void push_back(const T& x)
		{
			if (_finish == _end_of_storage)
			{
				reserve(_start == nullptr ? 4 : 2 * capacity());
			}
			*_finish = x;
			_finish++;
		}

		void pop_back()
		{
			assert(size() > 0);
			_finish--;
		}

		
		iterator insert(iterator pos, const T& x)
		{
			assert(pos >= begin() && pos <= end());
			int old_pos = pos - begin();
			if (capacity() == size())
			{
				reserve(capacity() == 0 ? 4 : 2 * capacity());
				pos = _start + old_pos;
			}
			for (iterator end = _finish ; end > pos; end--)
			{
				*end = *(end - 1);
			}
			*pos = x;
			_finish++;
			return pos;
			
		}

		iterator erase(iterator pos)
		{
			assert(pos >= begin() && pos <= end());
			for (iterator end = pos; end < _finish; end++)
			{
				*end = *(end + 1);
			}
			_finish--;
			return pos;
		}
	private:
		iterator _start = nullptr;
		iterator _finish = nullptr;
		iterator _end_of_storage = nullptr;

	};

	template<class T>
	void print_vector(const my_vector<T>& v)
	{
		assert(v.size() >= 0);
		typename my_vector<T>::const_iterator it = v.begin();
		for (int i = 0; i < v.size(); i++)
		{
			cout << *it++;
		}
		cout << endl;
	}

	template<class Container>
	void print_container(const Container& v)
	{
		for (auto e : v)
		{
			cout << e;
		}
		cout << endl;
	}
}